﻿#include "FITKAbaqusAdaptorPart.h"
#include "InpKeyLineSpliter.h"
#include "FITKAbaqusINPReader.h"
#include "FITKAbaqusINPWriter.h"
#include "FITKAbaqusAdaptorSet.h"
#include "FITKAbaqusAdaptorSurface.h"
#include "FITKAbaqusAdaptorSection.h"
#include "FITKAbaqusAdaptorInertia.h"
#include "FITKAbaqusAdaptorTransform.h"
#include "FITKAbaqusAdaptorElement.h"
#include "FITK_Component/FITKAbaqusData/FITKDataCase.h"
#include "FITK_Component/FITKAbaqusData/FITKAbaqusPart.h"

#include "FITK_Kernel/FITKCore/FITKVec3D.h"
#include "FITK_Kernel/FITKCore/FITKEnumTransformer.hpp"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMeshPartVTK.h"
#include "FITK_Interface/FITKInterfaceModel/FITKUnstructuredMesh.h"
#include "FITK_Interface/FITKInterfaceModel/FITKModelSet.h"
#include "FITK_Interface/FITKInterfaceModel/FITKComponentManager.h"

#include "FITK_Interface/FITKInterfaceStructural/FITKAbaSectionBeam.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaSectionShellHomogeneous.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaSectionSolidHomogeneous.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaAbstractSection.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaProfile.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaProfileI.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaProfileL.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaProfileBox.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaProfilePipe.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaProfileRectangular.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaProfileCircular.h"

#include "FITK_Interface/FITKInterfaceStructural/FITKAbaProfileManager.h"
#include "FITK_Interface/FITKInterfacePhysics/FITKAbstractSection.h"
#include "FITK_Interface/FITKInterfacePhysics/FITKSectionAssign.h"
#include "FITK_Interface/FITKInterfacePhysics/FITKAbstractMaterial.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaSectionAssignShellHomogeneous.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaSectionAssignSolidHomogenous.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaSectionAssignBeam.h"
#include "FITK_Interface/FITKInterfaceModel/FITKMeshSurface.h"
#include "FITK_Interface/FITKInterfaceModel/FITKMeshSurfaceNode.h"
#include "FITK_Interface/FITKInterfaceModel/FITKMeshSurfaceElement.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaInertiaPointMassInertia.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaEngineeringFeature.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaInertiaManager.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaTransform.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMaterialOrientation.h"
#include <QTextStream>
#include <QList>

//set集合每行不得超过16个，INP准则
#define SETCOUNT 16

//命名空间
namespace IO
{
    QString FITKAbaqusAdaptorPart::getAdaptorClass()
    {
        return "IO::FITKAbaqusAdaptorPart";
    }


    void FITKAbaqusAdaptorPart::setCaseData(AbaqusData::FITKDataCase* caseData)
    {
        _caseData = caseData;
    }

    bool FITKAbaqusAdaptorPart::adaptR()
    {
        _partData = AbaqusData::FITKAbaqusPart::SafeDownCastFITKAbaqusPart(_dataObj);
        if (_partData == nullptr || _reader == nullptr) return false;

        //回退一行 处理关键字信息
        _reader->backLine();

        QString line = _reader->readLine();
        if (!line.toLower().startsWith("*part")) return false;
        //关键字处理
        InpKeyLineSpliter s(line, _reader);
        QString partname = s["name"];
        _partData->setDataObjectName(partname);
        while (!_reader->atEnd())
        {
            bool readOK = false;
            line = _reader->readLine();
            if (!line.startsWith("*")) continue;

            s.reset(line);
            QString sH = s.getHeader().toLower();
            //发送计算进度信号
            _reader->sendCurrentPercent();
            //有不支持的关键字 异常退出
            //if (sH.isEmpty()) return false;

            if (sH == "end part") return true;
            else if (sH == "node")
            {
                readOK = this->readNode();
            }
            else if (sH == "element")
            {
                readOK = this->readElement();
                if (!readOK) return false;
                _reader->backLine();
                continue;
            }
            else if (_reader->getINPReadLevel() == INPReadLevel::ReadPart || _reader->getINPReadLevel() == INPReadLevel::ReadAssembly) continue;
            else if (sH == "nset" || sH == "elset")
            {
                readOK = this->readSet();
            }
            else if (sH == "surface")
            {
                readOK = this->readSurface();
            }
            else if (sH == "orientation")
            {
                readOK = this->readOrientation();
            }
            else if (sH == "beam section" || sH == "shell section" || sH == "solid section")
            {
                readOK = this->readSection();
            }
            else if (sH == "mass" || sH == "rotary inertia")
            {
                readOK = this->readInertia();
            }
            if (readOK)
            {
                _reader->backLine();
            }
            else
            {
                //解析出错目前不做处理
                //return false;
            }

        }
        return false;
    }

    bool FITKAbaqusAdaptorPart::adaptW()
    {
        _partData = AbaqusData::FITKAbaqusPart::SafeDownCastFITKAbaqusPart(_dataObj);
        if (_writer == nullptr || _partData == nullptr) return false;
        bool isW = true;
        // 写出函数主体
        QTextStream* stream = _writer->getStream();
        QString partName = _partData->getDataObjectName();
        partName = partName.contains(' ') ? QString("\"%1\"").arg(partName) : partName;
        *stream << "*Part, name=" << partName << endl;
        isW &= writeNode(_partData);
        isW &= writeElement(_partData);
        isW &= writeSetSurf(_partData);
        isW &= writeMaterialOrientation();
        isW &= writeSection(_partData);
        isW &= writeOrientation();
        isW &= writeInertia();
        //写出结尾标记
        *stream << "*End Part" << endl;
        *stream << "**" << endl;

        return isW;
    }

    bool FITKAbaqusAdaptorPart::readNode()
    {
        auto mesh = _partData->getMesh();
        if (mesh == nullptr) return false;
        //解析点 代码段
        while (!_reader->atEnd())
        {
            QString line = _reader->readLine();
            //关键字段结束
            if (line.startsWith("*")) return true;
            QStringList sList = line.split(",");
            Q_ASSERT(sList.size() == 4);

            if (sList.size() != 4)
            {
                //后续添加异常报错机制
                return false;
            }
            // 文件中点的编号
            int pointID = sList[0].toInt();
            // 节点坐标
            double x = sList[1].toDouble();
            double y = sList[2].toDouble();
            double z = sList[3].toDouble();
            mesh->addNode(pointID, x, y, z);
            _reader->sendCurrentPercent();
        }
        //未正常结束
        return false;

    }

    bool FITKAbaqusAdaptorPart::readElement()
    {
        auto adaptor = new FITKAbaqusAdaptorElement;
        if (!adaptor)return false;
        //设置文件读取器和存储的数据对象
        adaptor->setFileReader(_reader);
        adaptor->setDataObject(_partData);
        bool ok = adaptor->adaptR();
        if (!ok)
            _reader->consoleMessage(3, QString("Read Part Element Error : File:%1, Line:%2").arg(__FILE__).arg(__LINE__));
        delete adaptor;
        return ok;
    }

    bool FITKAbaqusAdaptorPart::readSet()
    {
        Interface::FITKModelSet* set = new Interface::FITKModelSet();
        auto adaptor = FITKIOADAPTORFACTORY->createT<FITKAbaqusAdaptorSet>("INP", set);
        if (adaptor == nullptr)
        {
            delete set;
            return false;
        }
        //设置读取器
        adaptor->setFileReader(_reader);
        adaptor->setCaseData(_caseData);
        adaptor->setDataObject(set);
        bool ok = adaptor->adaptR();

        if (adaptor != nullptr)
            delete adaptor;

        if (ok)
        {
            //设置模型
            set->setModel(_partData);
            //private私有函数，上面已经调用过了，故下面不做_partData的异常处理
            auto setManager = _partData->getComponentManager();
            setManager->appendDataObj(set);
            return true;
        }
        //未正常结束
        delete set;
        return false;
    }

    bool FITKAbaqusAdaptorPart::readSurface()
    {
        //回退一行 处理关键字信息 确定单元类型
        _reader->backLine();
        QString line = _reader->readLine();
        //关键字处理
        InpKeyLineSpliter s(line, _reader);
        QString type = s["type"].toLower();
        //创建存储数据对象
        Interface::FITKMeshSurface* surface = nullptr;
        if (type == "element")
        {
            surface = new Interface::FITKMeshSurfaceElement;
        }
        else if (type == "node")
        {
            surface = new Interface::FITKMeshSurfaceNode;
        }
        if (!surface)return false;
        surface->setModel(_dataObj->getDataObjectID());
        //表面读取器
        auto adaptor = FITKIOADAPTORFACTORY->createT<FITKAbaqusAdaptorSurface>("INP", surface);
        if (adaptor == nullptr)
        {
            _reader->consoleMessage(3, QString("Create Part AdaptorSurface Error. File:%1 Line:%2").arg(__FILE__).arg(__LINE__));
            delete surface;
            return false;
        }
        //设置文件读取器和存储的数据对象
        adaptor->setFileReader(_reader);
        adaptor->setDataObject(surface);
        adaptor->setCaseData(_caseData);
        bool ok = adaptor->adaptR();
        //读取完毕释放截面属性读取器
        if (adaptor != nullptr)
            delete adaptor;
        if (ok)
        {
            //读取成功将数据对象放入管理
            _reader->consoleMessage(1, QString("Read Part Surface : %1").arg(surface->getDataObjectName()));
            auto setsurfManager = _partData->getComponentManager();
            setsurfManager->appendDataObj(surface);
            return true;
        }
        _reader->consoleMessage(3, QString("Read Part Surface Error : %1, File:%2, Line:%3").arg(surface->getDataObjectName()).arg(__FILE__).arg(__LINE__));
        delete surface;
        return false;
    }

    bool FITKAbaqusAdaptorPart::readOrientation()
    {
        //回退一行 处理关键字信息
        _reader->backLine();
        QString line = _reader->readLine();
        //关键字处理
        InpKeyLineSpliter s(line, _reader);
        QString sH = s.getHeader().toLower();
        //创建存储数据对象
        Interface::FITKCoordinateSystem* coordinate = nullptr;
        if (sH == "orientation")
        {
            if (s["system"].toUpper() == "CYLINDRICAL")
                coordinate = new Interface::FITKAbaTransform(Interface::FITKCoordinateSystem::FCSysType::FCSCylinder);
            else if (s["system"].toUpper() == "SPHERICAL")
                coordinate = new Interface::FITKAbaTransform(Interface::FITKCoordinateSystem::FCSysType::FCSSphere);
            else if (s["system"].isEmpty() || s["system"].toUpper() == "RECTANGULAR")
                coordinate = new Interface::FITKAbaTransform(Interface::FITKCoordinateSystem::FCSysType::FCSCartesian);

            if (!coordinate)
                _reader->consoleMessage(3, QString("Read Orientation's type Use of illegal parameters Error : type=%1, File:%2, Line:%3").arg(s["type"]).arg(__FILE__).arg(__LINE__));
        }
        if (!coordinate)return false;
        //读取器
        auto adaptor = FITKIOADAPTORFACTORY->createT<FITKAbaqusAdaptorTransform>("INP", coordinate);
        if (adaptor == nullptr)
        {
            _reader->consoleMessage(3, QString("Create Orientation AdaptorConstraint Error. File:%1 Line:%2").arg(__FILE__).arg(__LINE__));
            delete coordinate;
            return false;
        }
        //设置文件读取器和存储的数据对象
        adaptor->setFileReader(_reader);
        adaptor->setDataObject(coordinate);
        adaptor->setModelDataCaseData(_partData, _caseData);
        bool ok = adaptor->adaptR();
        bool isGlobal = adaptor->isGlobalCsys();
        //读取完毕释放截面属性读取器
        if (adaptor != nullptr)
            delete adaptor;
        if (!ok)
        {
            //约束读取器
            _reader->consoleMessage(3, QString("Read Orientation Error : %1, File:%2, Line:%3").arg(coordinate->getDataObjectName()).arg(__FILE__).arg(__LINE__));
            delete coordinate;
            return false;
        }
        //读取成功将数据对象放入管理
        _reader->consoleMessage(1, "Read Orientation Constraint : " + coordinate->getDataObjectName());
        if (isGlobal)
            delete coordinate;
        else
        {
            auto transformManager = _partData->getCoordinateSystemManager();
            transformManager->appendDataObj(coordinate);
        }
        return true;
    }

    bool FITKAbaqusAdaptorPart::readSection()
    {
        if (!_reader || !_caseData)return false;
        auto sectionManager = _caseData->getSectionManager();
        if (!sectionManager)return false;
        //回退一行 处理关键字信息 确定截面类型
        _reader->backLine();
        QString line = _reader->readLine();
        //关键字处理
        InpKeyLineSpliter s(line, _reader);
        QString sH = s.getHeader().toLower();
        //创建存储数据对象和创建截面指派
        Interface::FITKAbaAbastractSection* section = nullptr;
        Interface::FITKSectionAssign* assign = nullptr;
        Interface::FITKAbaProfile* profile = nullptr;
        if (sH == "beam section")
        {
            section = new Interface::FITKAbaSectionBeam();
            assign = new Interface::FITKAbaSectionAssignBeam;
            QString sectionBeamType = s["section"].trimmed().toLower();
            if (sectionBeamType == "i")
                profile = new Interface::FITKAbaProfileI;
            else if (sectionBeamType == "l")
                profile = new Interface::FITKAbaProfileL;
            else if (sectionBeamType == "box")
                profile = new Interface::FITKAbaProfileBox;
            else if (sectionBeamType == "pipe" || sectionBeamType == "thick pipe")
                profile = new Interface::FITKAbaProfilePipe;
            else if (sectionBeamType == "rect")
                profile = new Interface::FITKAbaProfileRectangular;
            else if (sectionBeamType == "circ")
                profile = new Interface::FITKAbaProfileCircular;
        }
        else if (sH == "shell section")
        {
            if (s.hasKey("composite"))
                return false;
            else
                section = new Interface::FITKAbaSectionShellHomogeneous();
            assign = new Interface::FITKAbaSectionAssignShellHomogeneous;
        }
        else if (sH == "solid section")
        {
            if (s.hasKey("composite"))
                return false;
            else
                section = new Interface::FITKAbaSectionSolidHomogeneous();
            assign = new Interface::FITKAbaSectionAssignSolidHomogenous;
        }
        if (!section || !assign)
        {
            if (section) delete section;
            if (assign) delete assign;
            return false;
        }
        section->setDataObjectName(QString("Section-%1").arg(sectionManager->getDataCount() + 1));
        //截面属性读取器
        FITKAbaqusAdaptorSection* adaptor = FITKIOADAPTORFACTORY->createT<FITKAbaqusAdaptorSection>("INP", section);
        if (adaptor == nullptr)
        {
            _reader->consoleMessage(3, QString("Create Part AdaptorSection Error. File:%1 Line:%2").arg(__FILE__).arg(__LINE__));
            delete assign;
            delete section;
            return false;
        }
        //设置文件读取器和存储的数据对象
        adaptor->setFileReader(_reader);
        adaptor->setDataObject(section);
        adaptor->setSectionAssignAndProfile(assign, profile);
        adaptor->setModel(_partData);
        //开始读取
        bool ok = adaptor->adaptR();
        //读取完毕释放截面属性读取器
        if (adaptor != nullptr)
            delete adaptor;
        if (ok)
        {
            //读取成功将数据对象放入管理
            _reader->consoleMessage(1, "Read Part Section : " + section->getDataObjectName());
            sectionManager->appendDataObj(section);
            auto sectionAssignManager = _partData->getSectionAssignManager();
            sectionAssignManager->appendDataObj(assign);
            if (profile && _caseData->getProfileManager())
            {
                profile->setDataObjectName(QString("Profile-%1").arg(_caseData->getProfileManager()->getDataCount() + 1));
                _caseData->getProfileManager()->appendDataObj(profile);
            }
            return true;
        }
        //读取失败释放数据对象
        _reader->consoleMessage(3, QString("Read Part Section Error : %1, File:%2, Line:%3").arg(section->getDataObjectName()).arg(__FILE__).arg(__LINE__));
        if (profile)
            delete profile;
        delete assign;
        delete section;
        return false;
    }

    bool FITKAbaqusAdaptorPart::readInertia()
    {
        //回退一行 处理关键字信息
        _reader->backLine();
        QString line = _reader->readLine();
        //关键字处理
        InpKeyLineSpliter s(line, _reader);
        QString sH = s.getHeader().toLower();
        //创建存储数据对象
        Interface::FITKAbaAbstractInertia* inertia = nullptr;
        //点质量 与 转动惯量
        if (sH == "mass" || sH == "rotary inertia")
            inertia = new Interface::FITKAbaInertiaPointMassInertia;
        if (!inertia)return false;
        inertia->setModel(_dataObj->getDataObjectID());
        //惯性读取器
        auto adaptor = FITKIOADAPTORFACTORY->createT<FITKAbaqusAdaptorInertia>("INP", inertia);
        if (adaptor == nullptr)
        {
            _reader->consoleMessage(3, QString("Create Inertia AdaptorInertia Error. File:%1 Line:%2").arg(__FILE__).arg(__LINE__));
            delete inertia;
            return false;
        }
        //设置文件读取器和存储的数据对象
        adaptor->setFileReader(_reader);
        adaptor->setDataObject(inertia);
        adaptor->setPartData(_partData);
        bool ok = adaptor->adaptR();
        //读取完毕释放截面属性读取器
        if (adaptor != nullptr)
            delete adaptor;
        if (ok)
        {
            //读取成功将数据对象放入管理
            _reader->consoleMessage(1, "Read Inertia success : " + inertia->getDataObjectName());
            auto engineerFeature = _partData->getPartEngineerFeature();
            //惯性管理器
            Interface::FITKAbaInertiaManager* inertiaManager = engineerFeature->getInertiaManager();
            inertiaManager->appendDataObj(inertia);
            return true;
        }

        //约束读取器
        _reader->consoleMessage(3, QString("Read Inertia Error : %1, File:%2, Line:%3").arg(inertia->getDataObjectName()).arg(__FILE__).arg(__LINE__));
        delete inertia;
        return false;
    }

    bool FITKAbaqusAdaptorPart::writeNode(AbaqusData::FITKAbaqusPart * partData)
    {
        auto mesh = partData->getMesh();
        if (mesh == nullptr) return false;
        // 写出节点函数主体
        QTextStream* stream = _writer->getStream();
        *stream << "*Node" << endl;

        int nodeCount = mesh->getNodeCount();

        for (int i = 0; i < nodeCount; ++i)
        {
            auto node = mesh->getNodeAt(i);
            *stream << _writer->int2String(node->getNodeID(), 8);
            *stream << ",";
            *stream << _writer->double2String(node->x(), 9, 18);
            *stream << ",";
            *stream << _writer->double2String(node->y(), 9, 18);
            *stream << ",";
            *stream << _writer->double2String(node->z(), 9, 18);
            *stream << endl;
        }
        return true;
    }

    bool FITKAbaqusAdaptorPart::writeElement(AbaqusData::FITKAbaqusPart * partData)
    {
        auto adaptor = new FITKAbaqusAdaptorElement;
        if (!adaptor)return false;
        //设置文件读取器和存储的数据对象
        adaptor->setFileWriter(_writer);
        adaptor->setDataObject(partData);
        adaptor->setWriteElementId(&_writeElementId);
        bool ok = adaptor->adaptW();
        if (!ok)
            _writer->consoleMessage(3, QString("write Part Element Error : File:%1, Line:%2").arg(__FILE__).arg(__LINE__));
        delete adaptor;
        return ok;
    }

    bool FITKAbaqusAdaptorPart::writeSetSurf(AbaqusData::FITKAbaqusPart* partData)
    {
        auto setSurfManager = partData->getComponentManager();
        if (!setSurfManager)return false;
        int count = setSurfManager->getDataCount();
        bool ok = true;
        for (int i = 0; i < count; ++i)
        {
            auto data = setSurfManager->getDataByIndex(i);
            Interface::FITKModelSet* set = dynamic_cast<Interface::FITKModelSet*>(data);
            if (set)
                ok = writeSet(set);
            Interface::FITKMeshSurface* surface = dynamic_cast<Interface::FITKMeshSurface*>(data);
            if (surface)
                ok = writeSurface(partData, surface);
        }
        return ok;
    }

    bool FITKAbaqusAdaptorPart::writeSet(Interface::FITKModelSet * set)
    {
        bool ok = false;
        auto adaptor = FITKIOADAPTORFACTORY->createT<FITKAbaqusAdaptorSet>("INP", set);
        if (adaptor == nullptr) return false;
        adaptor->setFileWriter(_writer);
        adaptor->setDataObject(set);
        adaptor->setCaseData(_caseData);
        ok = adaptor->adaptW();
        delete adaptor;
        return ok;
    }

    bool FITKAbaqusAdaptorPart::writeSurface(AbaqusData::FITKAbaqusPart* partData, Interface::FITKMeshSurface* surface)
    {
        //调用surface适配器读写
        auto setSurfManager = partData->getComponentManager();
        if (!setSurfManager)return false;
        bool ok = false;
        auto adaptor = FITKIOADAPTORFACTORY->createT<FITKAbaqusAdaptorSurface>("INP", surface);
        if (adaptor == nullptr) return false;
        adaptor->setFileWriter(_writer);
        adaptor->setDataObject(surface);
        adaptor->setCaseData(_caseData);
        ok = adaptor->adaptW();
        delete adaptor;
        if (!ok) _writer->consoleMessage(3, QString("Write Part Surface Error,File:%1 Line:%2").arg(__FILE__).arg(__LINE__));
        return ok;
    }

    bool FITKAbaqusAdaptorPart::writeMaterialOrientation()
    {
        if (!_partData)return false;
        //写出材料指派方向
        Interface::FITKAbaMaterialOrientationManager* materialOrientationManager = _partData->getMaterialOrientationManager();
        int count = materialOrientationManager->getDataCount();
        for (int i = 0; i < count; ++i)
        {
            Interface::FITKAbaMaterialOrientation* transform = materialOrientationManager->getDataByIndex(i);
            if (!transform)continue;
            auto adaptor = FITKIOADAPTORFACTORY->createT<FITKAbaqusAdaptorTransform>("INP", transform);
            if (adaptor == nullptr) return false;
            //设置数据
            adaptor->setFileWriter(_writer);
            adaptor->setDataObject(transform);
            adaptor->setModelDataCaseData(_partData, _caseData, true);
            //开始写出
            bool ok = adaptor->adaptW();
            delete adaptor;
            if (!ok)
                _writer->consoleMessage(3, QString("write Orientation Error,File:%1 Line:%2").arg(__FILE__).arg(__LINE__));
            if (!ok) return false;
        }
        return true;
    }

    bool FITKAbaqusAdaptorPart::writeOrientation()
    {
        if (!_partData)return false;
        //写出局部坐标系
        auto transformManager = _partData->getCoordinateSystemManager();
        int count = transformManager->getDataCount();
        for (int i = 0; i < count; ++i)
        {
            Interface::FITKAbaTransform* transform = dynamic_cast<Interface::FITKAbaTransform*>(transformManager->getDataByIndex(i));
            if (!transform)continue;
            auto adaptor = FITKIOADAPTORFACTORY->createT<FITKAbaqusAdaptorTransform>("INP", transform);
            if (adaptor == nullptr) return false;
            //设置数据
            adaptor->setFileWriter(_writer);
            adaptor->setDataObject(transform);
            adaptor->setModelDataCaseData(_partData, _caseData);
            //开始写出
            bool ok = adaptor->adaptW();
            delete adaptor;
            if (!ok)
                _writer->consoleMessage(3, QString("write Transform Error,File:%1 Line:%2").arg(__FILE__).arg(__LINE__));
            if (!ok) return false;
        }
        return true;
    }

    bool FITKAbaqusAdaptorPart::writeSection(AbaqusData::FITKAbaqusPart* partData)
    {
        //调用section适配器读写
        auto sectionManager = _caseData->getSectionManager();
        if (!sectionManager) return false;
        auto sectionAssignManager = partData->getSectionAssignManager();
        if (!sectionAssignManager) return false;
        //通过指派器来获取section并写出
        int setDataCount = sectionAssignManager->getDataCount();
        for (int i = 0; i < setDataCount; ++i)
        {
            bool ok = false;
            auto sectionAssign = sectionAssignManager->getDataByIndex(i);
            if (!sectionAssign->isValid())continue;
            int sectionID = sectionAssign->getSectionID();
            auto s = sectionManager->getDataByID(sectionID);
            Interface::FITKAbaAbastractSection* section = dynamic_cast<Interface::FITKAbaAbastractSection*>(s);
            if (section == nullptr)continue;
            auto adaptor = FITKIOADAPTORFACTORY->createT<FITKAbaqusAdaptorSection>("INP", section);
            if (adaptor == nullptr) return false;
            //设置数据
            adaptor->setFileWriter(_writer);
            adaptor->setDataObject(section);
            adaptor->setSectionAssignAndProfile(sectionAssign, nullptr);
            adaptor->setModel(partData);
            ok = adaptor->adaptW();
            delete adaptor;
            if (!ok) _writer->consoleMessage(3, QString("Write Part Section Error,File:%1 Line:%2").arg(__FILE__).arg(__LINE__));
            if (!ok)return false;
        }
        return true;
    }

    bool FITKAbaqusAdaptorPart::writeInertia()
    {
        if (!_partData)return false;
        //获取装配下的工程特性
        Interface::FITKAbaPartEngineeringFeature* engineeringFeatureAssembly = _partData->getPartEngineerFeature();
        //获取惯量管理器
        Interface::FITKAbaInertiaManager* inertiaManager = engineeringFeatureAssembly->getInertiaManager();
        int count = inertiaManager->getDataCount();
        for (int i = 0; i < count; ++i)
        {
            Interface::FITKAbaAbstractInertia* inertia = inertiaManager->getDataByIndex(i);
            if (!inertia)continue;
            auto adaptor = FITKIOADAPTORFACTORY->createT<FITKAbaqusAdaptorInertia>("INP", inertia);
            if (adaptor == nullptr) return false;
            adaptor->setFileWriter(_writer);
            adaptor->setDataObject(inertia);
            adaptor->setWriteElementId(&_writeElementId);
            adaptor->setPartData(_partData);
            bool ok = adaptor->adaptW();
            delete adaptor;
            if (!ok)
                _writer->consoleMessage(3, QString("write Inertia Error,File:%1 Line:%2").arg(__FILE__).arg(__LINE__));
            if (!ok) return false;
        }
        return true;
    }
}

